package com.zl.csMall.indexShow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.csMall.basic.busFile.service.BusFileService;
import com.zl.csMall.indexShow.mapper.ShowCommodityMapper;
import com.zl.csMall.indexShow.po.ShowCommodity;
import com.zl.csMall.indexShow.service.ShowCommodityService;
import com.zl.csMall.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: ShowCommodityServiceImpl
 * @author: ChenNing
 * @date 2025-10-20 15:43
 */
@Service
public class ShowCommodityServiceImpl extends ServiceImpl<ShowCommodityMapper, ShowCommodity> implements ShowCommodityService {

    @Autowired
    private BusFileService busFileService;


    @Override
    public List<Map<String, Object>> getShowCommodityAll(LambdaQueryWrapper<ShowCommodity> queryWrapper) {
        List<Map<String, Object>> showCommodityList = this.listMaps(queryWrapper);
        int index = 0;
        for (Map<String, Object> showCommodityMap : showCommodityList){
            // 展示图片处理
            if (showCommodityMap.get("show_attachment_id") != null){
                long showAttachmentId = Long.parseLong(showCommodityMap.get("show_attachment_id").toString());
                List<String> showPicList = busFileService.getDowUrlByAttachmentId(showAttachmentId);
                showCommodityMap.put("showPicList", showPicList);
            }
            // 字段替换
            showCommodityMap.put("commodityId", showCommodityMap.get("commodity_id"));
            showCommodityMap.put("showAttachmentId", showCommodityMap.get("show_attachment_id"));
            showCommodityMap.remove("commodity_id");
            showCommodityMap.remove("show_attachment_id");
            showCommodityList.set(index, showCommodityMap);
            index++;
        }
        return showCommodityList;
    }

    @Override
    public boolean deleteById(long id) {
        LambdaQueryWrapper<ShowCommodity> showCommodityQuery = new LambdaQueryWrapper<>();
        showCommodityQuery.select(ShowCommodity::getId, ShowCommodity::getShowAttachmentId)
                .eq(ShowCommodity::getId, id);
        ShowCommodity showCommodity = this.getOne(showCommodityQuery, false);
        if (showCommodity == null || showCommodity.getId() == 0){
            return false;
        }
        boolean result = this.removeById(id);
        if (result){
            // 删除展示图片文件
            if (showCommodity.getShowAttachmentId() != null){
                busFileService.deleteByAttachmentId(showCommodity.getShowAttachmentId());
            }
        }
        return result;
    }

    @Override
    public List<String> getCommodityIndexBackground() {
        LambdaQueryWrapper<ShowCommodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShowCommodity::getIsCommodityIndexBackground, 1);
        ShowCommodity showCommodity = this.getOne(queryWrapper, false);

        if (showCommodity.getShowAttachmentId() != null){
            List<String> fileUrlList = busFileService.getDowUrlByAttachmentId(showCommodity.getShowAttachmentId());
            if (fileUrlList != null && fileUrlList.size() > 0){
                return fileUrlList;
            }
        }
        return new ArrayList<>();
    }

    @Override
    public boolean updateCommodityIndexBackground(MultipartFile[] fileArr) {
        Date date = new Date();
        // 上传文件
        long attachmentId = Util.getUuId();
        busFileService.saveBatchBusFile(attachmentId, fileArr);

        LambdaQueryWrapper<ShowCommodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShowCommodity::getIsCommodityIndexBackground, 1);
        ShowCommodity showCommodity = this.getOne(queryWrapper, false);
        if (showCommodity == null || showCommodity.getId() == 0){
            // 新增
            showCommodity = new ShowCommodity();
            showCommodity.setId(Util.getUuId());
            showCommodity.setShowAttachmentId(attachmentId);
            showCommodity.setIsCar(1);
            showCommodity.setIsCommodityIndexBackground(1);
            showCommodity.setCreateDate(date);
            showCommodity.setUpdateDate(date);
            return this.save(showCommodity);
        } else {
            // 更新附件ID
            showCommodity.setShowAttachmentId(attachmentId);
            showCommodity.setUpdateDate(date);
            return this.updateById(showCommodity);
        }
    }

}
